package by.grsu.coctails.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import by.grsu.coctails.R;
import by.grsu.coctails.adapter.CompositionAdapter;
import by.grsu.coctails.common.utils.ImageManager;
import by.grsu.coctails.model.Coctail;
import by.grsu.coctails.model.Ingredient;

import com.google.android.imageloader.ImageLoader;

public class CompositionFragment extends CommonReceiverFragment {

	public static final String TAG = CompositionFragment.class.getSimpleName();

	private static final String COCTAIL_KEY = "COCTAIL_KEY";

	private ImageView mImage;

	private TextView mHistory;

	private TextView mInstruction;

	private ImageLoader mImageLoader;

	private ListView mListView;

	private CompositionAdapter mAdapter;

	private TextView mTitle;

	private View mHeaderView;

	private LinearLayout mShareButton;

	public static CompositionFragment newInstance(Coctail coctail) {
		Bundle bundle = new Bundle();
		bundle.putParcelable(COCTAIL_KEY, coctail);
		CompositionFragment fragment = new CompositionFragment();
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageLoader = ImageLoader.get(getActivity());
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		final Coctail coctail = getArguments().getParcelable(COCTAIL_KEY);

		mHeaderView = getHeaderView();
		initHeaderView(coctail);

		mTitle = (TextView) getView().findViewById(R.id.title);
		mTitle.setText(coctail.getName());

		mListView = (ListView) getView().findViewById(R.id.list);
		mAdapter = new CompositionAdapter(getActivity(), R.layout.composition_item, coctail.getIngredients());

		mListView.addHeaderView(mHeaderView);
		mListView.setAdapter(mAdapter);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (position > 0) {
					Ingredient ingredient = mAdapter.getList().get(position - 1);
					FragmentCreator.addIngredientFragment(getActivity(), ingredient);
				}
			}
		});

		mShareButton = (LinearLayout) getView().findViewById(R.id.shareButton);
		mShareButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/html");
				intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.coctail) + " " + coctail.getName());
				intent.putExtra(Intent.EXTRA_TEXT, coctail.toString());

				startActivity(Intent.createChooser(intent, "Send Email"));
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.composition_fragment, container, false);
	}

	private void initHeaderView(Coctail coctail) {
		mImage = (ImageView) mHeaderView.findViewById(R.id.image);
		mHistory = (TextView) mHeaderView.findViewById(R.id.history);
		mInstruction = (TextView) mHeaderView.findViewById(R.id.instruction);

		mHistory.setText(coctail.getHistory());
		mInstruction.setText(coctail.getInstruction());
		mImageLoader.bind(mImage, ImageManager.HTTP + coctail.getImage(), null);

	}

	private View getHeaderView() {
		return LayoutInflater.from(getActivity()).inflate(R.layout.composition_header, null, false);
	}

	@Override
	protected void onIngredientChandged(String name, int state) {
		for (Ingredient ingredient : mAdapter.getList()) {
			if (ingredient.getName().equals(name)) {
				ingredient.setHaved(state);
				mAdapter.notifyDataSetChanged();
				break;
			}
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		unRegisterReceiver();
	}

	@Override
	public void onResume() {
		super.onResume();
		registerReceiver();
	}

}
